package model3d
{
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.maths.Point;
	import laya.ui.Image;
	
	public class RockerView extends Sprite
	{
		private var curTouchId:int;
		private var isDown:Boolean=false;
		//角度
		public static var angle:Number;
		//弧度
		public static var radians:Number;
		private var cPoint:Point;
		private var knob:Image;
		public function RockerView()
		{
			super();
			var bg:Image=new Image();
			bg.anchorX=0.5;
			bg.anchorY=0.5;
			this.addChild(bg);
			bg.pos(80,80);
			bg.size(160,160);
			bg.loadImage("comp/rocker_bg.png");
			knob=new Image();
			knob.loadImage("comp/img_yaogan.png");
			this.addChild(knob);
			knob.anchorX=0.5;
			knob.anchorY=0.5;
			knob.size(80,80);
			knob.pos(80,80);
			knob.alpha=0.6;
			this.size(160,160);
			radians=angle=-1;
			cPoint=new Point();
			cPoint.setTo(this.width/2,this.height/2);
			this.on(Event.MOUSE_DOWN,this,onMouseDown);
			
		}
		
		private function onMouseUp(e:Event):void
		{
			// TODO Auto Generated method stub
			if(curTouchId!=e.touchId) return ;
			isDown=false;
			knob.pos(cPoint.x,cPoint.y);
			//修改摇杆角度与弧度为-1（代表无角度）
			radians=angle=-1;
			Laya.stage.off(Event.MOUSE_MOVE,this,onMouseMove);
			Laya.stage.off(Event.MOUSE_UP,this,onMouseUp);
			Laya.stage.off(Event.MOUSE_OUT,this,onMouseUp);
			event("ROCKER_UP");
		}
		
		private function onMouseDown(e:Event):void
		{
			// TODO Auto Generated method stub
			curTouchId=e.touchId;
			isDown=true;
			Laya.stage.on(Event.MOUSE_MOVE,this,onMouseMove);
			Laya.stage.on(Event.MOUSE_UP,this,onMouseUp);
			Laya.stage.on(Event.MOUSE_OUT,this,onMouseUp);
			event("ROCKER_DOWN");
		}
		
		private function onMouseMove(e:Event):void
		{
			// TODO Auto Generated method stub
			if(e.touchId!=curTouchId) return;
			knob.pos(this.mouseX,this.mouseY);
			
			var deltaX:Number=knob.x-cPoint.x;
			var deltaY:Number=knob.y-cPoint.y;
			
			angle=Math.atan2(deltaX,deltaY)*180/Math.PI;
			if(angle<0) angle+=360;
			angle=Math.round(angle) ;
			//计算控制点在摇杆中的弧度
			radians= Math.PI / 180 * angle;
			if(Math.sqrt(deltaX*deltaX+deltaY*deltaY)>80){
				knob.pos(Math.sin(radians)*80+cPoint.x,Math.cos(radians)*80+cPoint.y);
			}
		}
	}
}